package com.smart.manage.service.impl;

import java.util.List;
import com.smart.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.smart.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.smart.manage.domain.TbPatientsConsultation;
import com.smart.manage.mapper.DoctorConsultationMapper;
import com.smart.manage.domain.DoctorConsultation;
import com.smart.manage.service.IDoctorConsultationService;

/**
 * 就诊管理Service业务层处理
 * 
 * @author lingyao
 * @date 2025-02-19
 */
@Service
public class DoctorConsultationServiceImpl implements IDoctorConsultationService 
{
    @Autowired
    private DoctorConsultationMapper doctorConsultationMapper;

    /**
     * 查询就诊管理
     * 
     * @param id 就诊管理主键
     * @return 就诊管理
     */
    @Override
    public DoctorConsultation selectDoctorConsultationById(Long id)
    {
        return doctorConsultationMapper.selectDoctorConsultationById(id);
    }

    /**
     * 查询就诊管理列表
     * 
     * @param doctorConsultation 就诊管理
     * @return 就诊管理
     */
    @Override
    public List<DoctorConsultation> selectDoctorConsultationList(DoctorConsultation doctorConsultation)
    {
        return doctorConsultationMapper.selectDoctorConsultationList(doctorConsultation);
    }

    /**
     * 新增就诊管理
     * 
     * @param doctorConsultation 就诊管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDoctorConsultation(DoctorConsultation doctorConsultation)
    {
        doctorConsultation.setCreateTime(DateUtils.getNowDate());
        int rows = doctorConsultationMapper.insertDoctorConsultation(doctorConsultation);
        insertTbPatientsConsultation(doctorConsultation);
        return rows;
    }

    /**
     * 修改就诊管理
     * 
     * @param doctorConsultation 就诊管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDoctorConsultation(DoctorConsultation doctorConsultation)
    {
        doctorConsultation.setUpdateTime(DateUtils.getNowDate());
//        doctorConsultationMapper.deleteTbPatientsConsultationByDoctorConsultationId(doctorConsultation.getId());
//        insertTbPatientsConsultation(doctorConsultation);
        return doctorConsultationMapper.updateDoctorConsultation(doctorConsultation);
    }

    /**
     * 批量删除就诊管理
     * 
     * @param ids 需要删除的就诊管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDoctorConsultationByIds(Long[] ids)
    {
        doctorConsultationMapper.deleteTbPatientsConsultationByDoctorConsultationIds(ids);
        return doctorConsultationMapper.deleteDoctorConsultationByIds(ids);
    }

    /**
     * 删除就诊管理信息
     * 
     * @param id 就诊管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDoctorConsultationById(Long id)
    {
        doctorConsultationMapper.deleteTbPatientsConsultationByDoctorConsultationId(id);
        return doctorConsultationMapper.deleteDoctorConsultationById(id);
    }

    /**
     * 新增就诊人问诊信息信息
     * 
     * @param doctorConsultation 就诊管理对象
     */
    public void insertTbPatientsConsultation(DoctorConsultation doctorConsultation)
    {
        List<TbPatientsConsultation> tbPatientsConsultationList = doctorConsultation.getTbPatientsConsultationList();
        Long id = doctorConsultation.getId();
        if (StringUtils.isNotNull(tbPatientsConsultationList))
        {
            List<TbPatientsConsultation> list = new ArrayList<TbPatientsConsultation>();
            for (TbPatientsConsultation tbPatientsConsultation : tbPatientsConsultationList)
            {
                tbPatientsConsultation.setDoctorConsultationId(id);
                if (tbPatientsConsultation.getCreateTime() == null){
                    tbPatientsConsultation.setCreateTime(DateUtils.getNowDate());
                }
                tbPatientsConsultation.setUpdateTime(DateUtils.getNowDate());
                list.add(tbPatientsConsultation);
            }
            if (list.size() > 0)
            {
                doctorConsultationMapper.batchTbPatientsConsultation(list);
            }
        }
    }
}
